
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.tool.wlfacade.helpers;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.management.ObjectName;

import oxygen.tool.wlfacade.WLFacadeContextHelper;
import oxygen.util.OxygenUtils;

/**
 * Monitor an attribute
 * Can send the monitoring information to a file
 * Store all WLMonitorRunnable objects in an attribute of the wlctx (a hashmap)
 * Thus, when we say stopmonitoring, it can iterate through and stop monitoring 
 * everything.
 * Also, we can just stop monitoring any single one
 */
public class WLMonitorAttribute {
  
  private static Map getMonitorsMap(WLFacadeContextHelper wlctx) throws Exception {
    String key = WLMonitorAttribute.class.getName() + "-ATTS";
    Map m = (Map)wlctx.getFacadeContext().getAttribute(key);
    if(m == null) {
      m = new HashMap();
      wlctx.getFacadeContext().setAttribute(key, m);
    }
    return m;
  }
  
  /** returns a unique string, which can be used to later shutdown the monitoring */
  public static String monitor(WLFacadeContextHelper wlctx, ObjectName objname, String[] attnames, 
                               int interval, int numtimes, OutputStream os, boolean doClose) throws Exception {
    Map m = getMonitorsMap(wlctx);
    String uid = String.valueOf(System.currentTimeMillis());
    WLMonitorRunnable r = new WLMonitorRunnable();
    m.put(uid, r);
    Thread thr = new Thread(OxygenUtils.topLevelThreadGroup(), r);
    thr.start();
    return uid;
  }

  //monitors to System.out
  //stops when environment value is received (when wlctx.getEnvironment().rawInput('Hit enter to quit: ') is received
  public static void monitor(WLFacadeContextHelper wlctx, ObjectName objname, String[] attnames, 
                             int interval, int numtimes) throws Exception {    
    //Map m = getMonitorsMap(wlctx);
    //String uid = String.valueOf(System.currentTimeMillis());
    WLMonitorRunnable r = new WLMonitorRunnable();
    Thread thr = new Thread(OxygenUtils.topLevelThreadGroup(), r);
    thr.start();
    thr.join();
    //String s = wlctx.getEnvironment().rawInput("Hit Enter to stop");
    r.stop();
  }

  public static void stopmonitoring(WLFacadeContextHelper wlctx) throws Exception {
    Map m = getMonitorsMap(wlctx);
    for(Iterator itr = m.keySet().iterator(); itr.hasNext(); ) {
      WLMonitorRunnable r = (WLMonitorRunnable)m.get(itr.next());
      r.stop();
    }
    m.clear();
  }
  
  public static void stopmonitoring(WLFacadeContextHelper wlctx, String id) throws Exception {
    Map m = getMonitorsMap(wlctx);
    WLMonitorRunnable r = (WLMonitorRunnable)m.remove(id);
    r.stop();
  }

  
  public static class WLMonitorRunnable implements Runnable {
    private boolean stopped;
    public WLMonitorRunnable() { }
    public WLMonitorRunnable(WLFacadeContextHelper wlctx, ObjectName objname, String[] attnames, 
                             int interval, int numtimes, OutputStream os, boolean doClose) {

    }
    
    public void run() {
      while(!stopped) {

      }
      stop();
    }

    public void stop() {
      stopped = true;
    }
  }
}

